﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Org;

namespace ZHDJ.Web.CommonMethod
{
    public class StatisticMethod
    {
        /// <summary>
        /// 根据当前日期和UserID更新统计表的是否在线状态
        /// </summary>
        /// <param name="IsOnline"></param>
        /// <param name="LoginID"></param>
        public static void Updatepartyvisitstatistictwo(bool IsOnline, string LoginID)
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
                {
                    new MySqlParameter() { DbType=DbType.Boolean,ParameterName="IsOnline",Value=IsOnline },
                    new MySqlParameter() { DbType=DbType.String,ParameterName="UserID",Value=LoginID },
                    new MySqlParameter() { DbType=DbType.DateTime,ParameterName="AddDate",Value=DateTime.Now.ToString("yyyy-MM-dd") }
                };

            string sql = " UPDATE tblpartyvisitstatisticlogin SET `IsOnline`=@IsOnline WHERE UserID=@UserID AND AddDate=@AddDate ";
            Data.MySqlHelper.ExecuteNonQuery(sql, parameters.ToArray());
        }

        public static void Addpartyvisitstatistictwo(PartyOrgMember user, bool IsOnline)
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
                    {
                        new MySqlParameter() { DbType = DbType.String, ParameterName = "ID",Value = StringHelper.NewID},
                        new MySqlParameter() { DbType = DbType.String, ParameterName = "UserID",Value = user.LoginID},
                        new MySqlParameter() { DbType = DbType.String, ParameterName = "BranchID",Value = user.BranchID},
                        new MySqlParameter() { DbType = DbType.Boolean, ParameterName = "IsLogin",Value = true},
                        new MySqlParameter() { DbType = DbType.Boolean, ParameterName = "IsOnline",Value = IsOnline},
                        new MySqlParameter() { DbType = DbType.DateTime, ParameterName = "AddDate",Value = DateTime.Now.ToString("yyyy-MM-dd")}
                    };

            string sql = " INSERT INTO `tblpartyvisitstatisticlogin` (`ID`, `UserID`, `BranchID`, `IsLogin`, `IsOnline`, `AddDate`) VALUES (@ID, @UserID, @BranchID, @IsLogin, @IsOnline, @AddDate) ";
            Data.MySqlHelper.ExecuteNonQuery(sql, parameters.ToArray());
        }

        /// <summary>
        /// 统计登录人数
        /// </summary>
        /// <param name="user"></param>
        public static void StatisticLogin(PartyOrgMember user)
        {
            /*
             * 是否登录
             * 1.根据当前日期和UserID查询表中是否有数据
             * 2.若没有则添加，否则不操作
             * 是否在线
             * 1.根据当前日期和UserID查询表中是否有数据
             * 2.若没有则添加，否则根据当前日期和UserID更新当前登录人的在线状态为true
             */
            string datetime = DateTime.Now.ToString("yyyy-MM-dd");
            DataTable dt = Data.MySqlHelper.ExecuteDataSet(string.Format("select * from tblpartyvisitstatisticlogin where UserID='{0}' and AddDate='{1}'", user.LoginID, datetime)).Tables[0];
            if (dt.Rows.Count == 0)
                Addpartyvisitstatistictwo(user, true);
            else
                Updatepartyvisitstatistictwo(true, user.LoginID);
        }

        /// <summary>
        /// 统计在线人数
        /// </summary>
        public static void StatisticOnLine(PartyOrgMember user)
        {
            /*
             * 1.汇总"tblpartyvisitstatisticlogin"表的在线人数，根据当前登录人的支部ID&&当前日期&&状态为在线
             * 2.若"tblpartyvisitstatisticonline"表中的在线峰值人数>汇总的在线峰值人数，不操作
             * 否则，更新"tblpartyvisitstatisticonline"表中的在线峰值人数的值=汇总的在线峰值人数
             * 3.若"tblpartyvisitstatisticonline"表中无数据，根据当前日期查询
             * 则直接添加汇总的在线峰值人数
             */
            string datetime = DateTime.Now.ToString("yyyy-MM-dd");
            DataTable dt = Data.MySqlHelper.ExecuteDataTable(string.Format(" select * from tblpartyvisitstatisticonline where BranchID='{0}' AND Adddate='{1}' ", user.BranchID, datetime));

            object obj = Data.MySqlHelper.ExecuteScalar(string.Format(" select COUNT(ID) from tblpartyvisitstatisticlogin where BranchID='{0}' AND Adddate='{1}' AND IsOnline=TRUE  GROUP BY BranchID ", user.BranchID, datetime));
            int logincount = Convert.ToInt32(obj);
            if (dt.Rows.Count > 0)
            {
                DataTable onlinedt = Data.MySqlHelper.ExecuteDataTable(string.Format(" select ID,OnlineNumber from tblpartyvisitstatisticonline where BranchID='{0}' AND Adddate='{1}' GROUP BY BranchID ", user.BranchID, datetime));

                int onlinecount = Convert.ToInt32(onlinedt.Rows[0]["OnlineNumber"]);

                if (onlinecount < logincount)
                {
                    string sql = string.Format(" UPDATE `tblpartyvisitstatisticonline` SET `OnlineNumber`={0} WHERE `ID`='{1}' ", logincount, onlinedt.Rows[0]["ID"].ToString());
                    Data.MySqlHelper.ExecuteNonQuery(sql);
                }
            }
            else
            {
                List<MySqlParameter> parameters = new List<MySqlParameter>()
                {
                    new MySqlParameter() { DbType=DbType.String,ParameterName="ID",Value=StringHelper.NewID},
                    new MySqlParameter() { DbType=DbType.String,ParameterName="BranchID",Value=user.BranchID},
                    new MySqlParameter() { DbType=DbType.Int32,ParameterName="OnlineNumber",Value=logincount},
                    new MySqlParameter() { DbType=DbType.DateTime,ParameterName="AddDate",Value=datetime}
                };
                string sql = " INSERT INTO `tblpartyvisitstatisticonline` (`ID`, `BranchID`, `OnlineNumber`, `AddDate`) VALUES (@ID, @BranchID, @OnlineNumber, @AddDate) ";
                Data.MySqlHelper.ExecuteNonQuery(sql, parameters.ToArray());
            }
        }


    }
}